<?php
namespace app\index\controller;

use think\Db;
class Set extends Base{
    protected $userId;
    public function _initialize(){
        parent::_initialize();
        $this->userId=cookie('user_id');
    }
    public function index(){
        $this->assign('user',Db::name('users')->where('id',$this->userId)->field('is_real,avatar,nickname')->find());
        return view();
    }
    // 修改昵称
    public function nickname(){
        return view();
    }
    // 修改登录密码
    public function loginPassword(){
        if (request()->isPost()){
            $new_password = input('post.new_password');
            $new_password_1 = input('post.new_password_1');
            if ($new_password == '' || $new_password_1 == ''){
                return json(['msg' => '密码不能为空' ,'state' => -1]);
            }
            if ($new_password_1 != $new_password){
                return json(['msg' => '两次密码不一致' ,'state' => -2]);
            }
            Db::name('users')->where('id',$this->userId)->setField('password',$new_password);
            return json(['msg' => '新密码设置成功','state' => 1]);
        }
        $user=Db::name('users')->where('id',$this->userId)->find();
        $this->assign([
            'user' => $user,
        ]);
        return view('login_password');
    }
    // 交易密码
    public function tradePassword(){
        if (request()->isPost()){
            $login_password=input('post.login_password');
            $trade_password= input('post.trade_password');
            $trade_password_1=input('post.trade_password_1');
            $user=Db::name('users')->where('id',$this->userId)->find();
            if ($login_password != $user['password']){
                return json(['msg' => '登录密码不正确','state' => -1]);
            }
            if ($trade_password != $trade_password_1){
                return json(['msg' => '两次密码不一致','state' => -2]);
            }
            if (strlen(trim($trade_password)) != 6){
                return json(['msg' => '请输入6位交易密码','code' => -3]);
            }
            Db::name('users')->where('id',$this->userId)->setField('trade_password',$trade_password);
            return json(['msg' => '交易密码设置成功','state' => 1]);
        }
        return view('trade_password');
    }
    // 消息管理
    public function message(){
        $this->assign([
            'msg' => Db::name('message')->order('add_time desc')->where('user_id',$this->userId)->select(),
        ]);
        return view();
    }
    // 修改呢称
    public function modifyNickname(){
        $nickname=input('post.nickname');
        $res=Db::name('users')->where('id',$this->userId)->setField('nickname',$nickname);
        if ($res){
            return json(['msg' => '修改成功' ,'state' => 1]);
        }
        return json(['msg' => '修改失败' ,'state' => -1]);
    }
    // 查看消息的详情
    public function messageDetail(){
        $id = input('id');
        $msg=Db::name('message')->where('id',$id)->find();
        $this->assign('msg',$msg);
        return view('message_detail');
    }
    // 头像修改

    // 语言选择
    public function lang(){
        return view();
    }
    // 上传头像
    public function uploadAvatar(){
        $img = $_POST['imgbase64'];
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $result)) {
            $type = ".".$result[2];
            $path = "./static/uploads/" . date("Y-m-d") . "-" . uniqid() . $type;
        }
        $img =  base64_decode(str_replace($result[1], '', $img));
        @file_put_contents($path, $img); // file_put_contents 目录不存在时文件上传不会成功
        Db::name('users')->where('id',$this->userId)->setField('avatar',str_replace('./','/',$path));
        return json(['src' => $path]);
    }
}